<?php


namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use \app\admin\model\user\dyfz\qita\Info as InfoModel;
use \app\admin\model\user\dyfz\qita\Log as LogModel;;


class Qitaxiang extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取年度学业列表
     *
     */
    public function list()
    {
        $list = InfoModel::where('status', 'normal')
            ->order('id', 'desc')
            ->paginate()
            ->each(function ($qita, $key) {
                $qita->qita = "";
                $stu = LogModel::where(['user_id' => $this->auth->id])
                    ->where(['s_id' => $qita->id])
                    ->find();
                if ($stu) {
                    $qita->qita = $stu;
                }
                $qita->fill = 0;
                if ($qita->start_time < time() && $qita->end_time > time()) {
                    $qita->fill = 1;
                }
            });
        $this->success('获取成功', $list);
    }

    /**
     * 提交奖状信息
     */
    public function submit()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post();
            $params['user_id'] = $this->auth->id;
            // $json_decoded = htmlspecialchars_decode($params['content']);
            // $params['content'] = json_decode($json_decoded);
            // // $this->success('提交成功', $params['content']);
            // $params['content'] = json_encode($params['content'], true);
            if (isset($params["id"])) {
                unset($params['create_time']);
                unset($params['create_time_text']);
                unset($params['update_time_text']);
                $params['update_time'] = time();
                $record = LogModel::where('id', $params["id"])->update($params);
            } else {
                $params['create_time'] = time();
                $record = LogModel::insert($params);
            }
            if ($record) {
                $this->success('提交成功');
            }
            $this->error('提交失败');
        }
        $this->error('非法请求');
    }

    /**
     * 获取奖状信息
     */
    public function info($id)
    {
        $awards = InfoModel::where('id', $id)
            ->find();
        $this->success('获取成功', $awards);
    }
}
